﻿using System;
using System.Resources;
using Microsoft.Win32;
using System.Diagnostics;

namespace posmoon.Tree
{
    class RegistrytSaver
    {
        #region

        private RegistryKey _regkey;

        #endregion

        public RegistrytSaver()
        {
            _regkey = Registry.CurrentUser;
        }

        public RegistrytSaver(RegistryKey key)
        {
            _regkey = key;
        }

        public bool WriteValue(string name, string ip, string defdb, string port, string user, string pass)
        {
            WriteToRegistry(name, ip, defdb, port, user, pass);
            return true;
        }

        public bool ReadValues(ref string ip, ref string defdb, ref string port, ref string user, ref string pass)
        {
            ReadFromRegistry(ref ip,ref defdb, ref port, ref user, ref pass);
            return true;
        }

        private void WriteToRegistry(string name, string ip, string defdb, string port, string user, string pass)
        {
            _regkey.CreateSubKey("Software\\posmoon\\" + name);
            _regkey.Close();

            _regkey = Registry.CurrentUser.OpenSubKey("Software\\posmoon\\" + name, true);

            _regkey.SetValue("{FFD0EA66-B1F3-43D8-B76F-6B1BAAF3AE6C}", ip);
            _regkey.SetValue("{67DACBFA-7B94-4579-8994-E9697252E037}", defdb);
            _regkey.SetValue("{66094CAC-B0FA-48F2-9C43-EFAEAFBA54DE}", port);
            _regkey.SetValue("{53F78477-0E88-4677-A9F4-B24791F5DCF6}", user);
            _regkey.SetValue("{668B3C5F-7E61-40F9-A3A5-019A94A988A0}", pass);
            
            _regkey.Close();
        }

        public bool WriteValue(string name, string user, string pass)
        {
            WriteToRegistry(name, user, pass);
            return true;
        }

        private void WriteToRegistry(string name, string user, string pass)
        {
            _regkey.CreateSubKey("Software\\posmoon\\" + name);
            _regkey.Close();

            _regkey = Registry.CurrentUser.OpenSubKey("Software\\posmoon\\" + name, true);

            _regkey.SetValue("{FFD0EA66-B1F3-43D8-B76F-6B1BAAF3AE6C}", user);
            _regkey.SetValue("{67DACBFA-7B94-4579-8994-E9697252E037}", pass);
            _regkey.Close();
        }

        private void ReadFromRegistry(ref string ip, ref string defdb, ref string port, ref string user, ref string pass)
        {
            ip = _regkey.GetValue("{FFD0EA66-B1F3-43D8-B76F-6B1BAAF3AE6C}").ToString();
            defdb = _regkey.GetValue("{67DACBFA-7B94-4579-8994-E9697252E037}").ToString();
            port = _regkey.GetValue("{66094CAC-B0FA-48F2-9C43-EFAEAFBA54DE}").ToString();
            user = _regkey.GetValue("{53F78477-0E88-4677-A9F4-B24791F5DCF6}").ToString();
            pass = _regkey.GetValue("{668B3C5F-7E61-40F9-A3A5-019A94A988A0}").ToString();

            _regkey.Close();
        }
    }
}
